import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/common/entitys/entitys.dart';
import 'package:flutter_base/common/provider/provider.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:package_info/package_info.dart';

import 'common/values/values.dart';

/// 全局配置
class Global {
  /// 用户配置
  static UserResponseEntity profile = UserResponseEntity(
    token: null,
    // token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MzY1Mjk0MTAsImlkIjoidGVzdCIsIm1vYmlsZSI6IiIsIm9yaWdfaWF0IjoxNjMzOTM3NDEwLCJ3ZWIiOiJhMGMwOWNmYTkwNGE1NmM1OGRjZTI1ZWQ3YWI3N2Y2MiJ9.M9lBGCNNuZTQVuxXVpsEOvCZes-59pAC3QA3EKeWlZU',
  );

  /// 保存用户名
  static String? userCode = '';
  static String? userName = '';
  static String? deptCode = '';
  static String? deptName = '';
  static String? userMobile = '';
  static String? userPsw = '';

  /// 设备号
  static String deviceCode = '';
  static String version = '';

  /// 是否第一次打开
  static bool isFirstOpen = false;

  /// 是否离线登录
  static bool isOfflineLogin = false;

  /// 应用状态
  static AppState appState = AppState();

  /// 是否 release
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  /// init
  static Future init() async {
    // 运行初始
    WidgetsFlutterBinding.ensureInitialized();

    // 工具初始
    await StorageUtil.init();
    await getDeviceInfo();
    await getPackageInfo();


    ///极光推送
    await ATPush.initPlatformState();

    HttpUtil();

    //状态栏黑色
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);

    //竖屏
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

    // 读取设备第一次打开
    isFirstOpen = !StorageUtil().getBool(STORAGE_DEVICE_ALREADY_OPEN_KEY);
    if (isFirstOpen) {
      StorageUtil().setBool(STORAGE_DEVICE_ALREADY_OPEN_KEY, true);
    }

    // 读取离线用户信息
    var _profileJSON = StorageUtil().getJSON(STORAGE_USER_PROFILE_KEY);
    if (_profileJSON != null) {
      profile = UserResponseEntity.fromJson(_profileJSON);
      isOfflineLogin = true;
    }

    // http 缓存

    // android 状态栏为透明的沉浸
    if (Platform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle =
      SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  }

  // 持久化 用户信息
  static Future<bool> saveProfile(UserResponseEntity userResponse) {
    profile = userResponse;
    return StorageUtil()
        .setJSON(STORAGE_USER_PROFILE_KEY, userResponse.toJson());
  }


  static getDeviceInfo() async {
    DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
    if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      print(iosDeviceInfo.identifierForVendor);
      deviceCode = iosDeviceInfo.identifierForVendor;
    } else if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      print('androidDeviceInfo.androidId:${androidDeviceInfo.androidId}');
      print('androidDeviceInfo.model:${androidDeviceInfo.model}');
      deviceCode = androidDeviceInfo.androidId;
    }
  }

  static getPackageInfo() async {
    PackageInfo info = await PackageInfo.fromPlatform();
    version = info.version;
  }
}
